import { $planet_DataManager_poof$ } from "../../../frame/ALIB/lib.data";
import { $planet_APP_NAME_poof$ } from "../../../frame/frame.config";
import { $planet_PLANET_ID_LIST_poof$, $planet_TARGET_PLANET_LIST_poof$ } from "../../config.global";
import { $planet_NCAPIURI_poof$, $planet_NCRequestParam_poof$ } from "../api/api.config.client";
import $planet_ServiceAPI_poof$ from "../api/service.api";
import $planet_Config_poof$ from "../manager/config.dynamic";
import $planet_get_proxy_status_poof$ from "./data.status";
import $planet_get_proxy_trigger_poof$ from "./data.trigger";
import $planet_get_proxy_user_poof$ from "./data.user";

export default class $planet_DC_poof$ {
	private static $planet___trigger___poof$: ReturnType<typeof $planet_get_proxy_trigger_poof$>;
	private static $planet___user___poof$: ReturnType<typeof $planet_get_proxy_user_poof$>;
	private static $planet___status___poof$: ReturnType<typeof $planet_get_proxy_status_poof$>;

	public static get $planet_trigger_poof$(){
		return $planet_DC_poof$.$planet___trigger___poof$;
	}
	public static get $planet_user_poof$(){
		return $planet_DC_poof$.$planet___user___poof$;
	}
	public static get $planet_status_poof$(){
		return $planet_DC_poof$.$planet___status___poof$;
	}

	public static get $planet_target_planet_poof$(){
		return $planet_DC_poof$.$planet___user___poof$.$planet_target_planet_poof$ as typeof $planet_PLANET_ID_LIST_poof$[number];
	}

	public static $planet_init_poof$(){
		$planet_DC_poof$.$planet___trigger___poof$ = $planet_get_proxy_trigger_poof$('$planet_game_trigger_poof$', $planet_APP_NAME_poof$);
		$planet_DC_poof$.$planet___user___poof$ = $planet_get_proxy_user_poof$('$planet_game_user_poof$', $planet_APP_NAME_poof$);
		$planet_DC_poof$.$planet___status___poof$ = $planet_get_proxy_status_poof$('$planet_game_status_poof$', $planet_APP_NAME_poof$);

		while($planet_DC_poof$.$planet_follow_cache_poof$.length>0){
			let [pxi_uri, key, call, update, np] = $planet_DC_poof$.$planet_follow_cache_poof$.pop();
			//@ts-ignore
			$planet_DC_poof$.$planet_follow_poof$(pxi_uri, key, call, update).then(reg=>np.$planet_resolve_poof$(reg));
		}
	}

	private static $planet_follow_cache_poof$: [string, string, Function, boolean, abd.$planet_NudityPromise_poof$<ReturnType<typeof $planet_DataManager_poof$.$planet_follow_poof$>>][] = [];
    /**
     * 注册监听
     * @param data 要监听的数据
     * @param key 要监听的字段
     * @param call 更新时触发的回调函数
     * @param update 是否在注册时触发回调
     */
	public static $planet_follow_poof$<P extends Exclude<abd.$planet_OmitKeys_poof$<typeof $planet_DC_poof$, Function>, 'prototype'>, D extends typeof $planet_DC_poof$[P], K extends abd.$planet_OmitKeys_poof$<D, Function>>(pxi_uri: P, key: K, call: (o: D[K], v: D[K])=>void, update?: boolean){
		let pxi = Reflect.get($planet_DC_poof$, pxi_uri);
		let np = new abd.$planet_NudityPromise_poof$<ReturnType<typeof $planet_DataManager_poof$.$planet_follow_poof$>>();
		update = update??true;
		if(!pxi){
			//@ts-ignore
			$planet_DC_poof$.$planet_follow_cache_poof$.unshift([pxi_uri, key, call, update, np]);
		}else{
			//@ts-ignore
			np.$planet_resolve_poof$($planet_DataManager_poof$.$planet_follow_poof$(pxi, key, call, update));
		}

		return np.$planet_promise_poof$;
	}

	/**
	 * 字符串格式化
	 * @param symbol
	 */
	public static $planet_symbolString_poof$(symbol?: number){
		let deco = ['.', ',', ' '];
		let config = $planet_Config_poof$.$planet_countryConfig_poof$;
		symbol = abd.$planet_number_poof$.$planet_fixedNum_poof$(symbol ?? $planet_DC_poof$.$planet_user_poof$.$planet_symbol_poof$, config.$planet_round_poof$ ? 0 : 2, true);
		let [str, DPart] = symbol.toString().split('.');
		if($planet_Config_poof$.$planet_CountryIs_poof$(12) && str.length>3){
			str = [str.slice(0, str.length-3).replace(/\B(?=(\d{2})+(?!\d))/g, deco[config.$planet_thousand_poof$-1]), str.slice(str.length-3)].join(deco[config.$planet_thousand_poof$-1]);
		}else{
			str = str.replace(/\B(?=(\d{3})+(?!\d))/g, deco[config.$planet_thousand_poof$-1]);
		}
		if(DPart) str = [str, DPart].join(deco[config.$planet_point_poof$-1]);
		str = (config.$planet_front_poof$ ? [config.$planet_symbol_poof$, str] : [str, config.$planet_symbol_poof$]).join(' ');

		return str;
	}

	public static $planet_parseSymbol_poof$(symbol?: number){
		let config = $planet_Config_poof$.$planet_countryConfig_poof$;
		symbol = abd.$planet_number_poof$.$planet_fixedNum_poof$(symbol ?? $planet_DC_poof$.$planet_user_poof$.$planet_symbol_poof$, config.$planet_round_poof$ ? 0 : 2, true);
		return symbol;
	}

	/** 刷新下一个目标球 */
	public static $planet_nextTargetPlanet_poof$(){
		let index = Math.min($planet_TARGET_PLANET_LIST_poof$.length-1, $planet_TARGET_PLANET_LIST_poof$.indexOf($planet_DC_poof$.$planet_target_planet_poof$)+1);
		$planet_DC_poof$.$planet_user_poof$.$planet_target_planet_poof$ = $planet_TARGET_PLANET_LIST_poof$[index];
		return $planet_DC_poof$.$planet_target_planet_poof$;
	}

	/** 获取下一个目标分值 */
	public static $planet_getNextTargetScore_poof$(){
		return $planet_DC_poof$.$planet_user_poof$.$planet_score_target_poof$ * 2;
	}

	private static $planet_force_pop_record_poof$ = {
		offset_index: 0,
		last_force: undefined,
		start: undefined,
	};
	/** 检测并记录强弹 */
	public static $planet_checkForcePop_poof$(): $planet_NCRequestParam_poof$<$planet_NCAPIURI_poof$.$planet_AD_poof$, '$planet_type_poof$'>{
		let count = $planet_DC_poof$.$planet_user_poof$.$planet_rp_count_poof$;
		if(!$planet_DC_poof$.$planet_force_pop_record_poof$.start) $planet_DC_poof$.$planet_force_pop_record_poof$.start = count;
		if(!$planet_DC_poof$.$planet_force_pop_record_poof$.last_force) $planet_DC_poof$.$planet_force_pop_record_poof$.last_force = $planet_DC_poof$.$planet_force_pop_record_poof$.start-1;

		let index = $planet_Config_poof$.$planet_otherConfig_poof$.$planet_forcePop_poof$.findIndex(el=>count.$planet_inRange_poof$(el.$planet_a_poof$, el.$planet_b_poof$));
		if(index<0) index = $planet_Config_poof$.$planet_otherConfig_poof$.$planet_forcePop_poof$.length-1;
		let conf = $planet_Config_poof$.$planet_otherConfig_poof$.$planet_forcePop_poof$[index];

		if(count<=conf.$planet_a_poof$) $planet_DC_poof$.$planet_force_pop_record_poof$.offset_index = 0;

		let offset = count - $planet_DC_poof$.$planet_force_pop_record_poof$.last_force;

		let force = offset >= conf.$planet_interval_poof$[$planet_DC_poof$.$planet_force_pop_record_poof$.offset_index];

		let type: $planet_NCRequestParam_poof$<$planet_NCAPIURI_poof$.$planet_AD_poof$, '$planet_type_poof$'>;
		if(force){
			$planet_DC_poof$.$planet_status_poof$.$planet_has_force_poof$ = true;
			type = $planet_DC_poof$.$planet_status_poof$.$planet_videoCount_poof$<5 ? 'full-screen' : 'incentive';
			$planet_DC_poof$.$planet_force_pop_record_poof$.offset_index = ($planet_DC_poof$.$planet_force_pop_record_poof$.offset_index+1) % conf.$planet_interval_poof$.length;
			$planet_DC_poof$.$planet_force_pop_record_poof$.last_force = count;
		};

		return type;
	}

	public static async $planet_synchUserInfo_poof$(){
		let result = await $planet_ServiceAPI_poof$.$planet_userInfo_poof$();
		if(!result) return void 0;

		$planet_DC_poof$.$planet_user_poof$.$planet_level_poof$ = result.$planet_level_poof$;
		$planet_DC_poof$.$planet_user_poof$.$planet_elsb_day_poof$ = result.$planet_elsbCount_poof$;
		$planet_DC_poof$.$planet_user_poof$.$planet_symbol_day_poof$ = result.$planet_symbolCount_poof$;
		$planet_DC_poof$.$planet_user_poof$.$planet_dayElsb_poof$ = result.$planet_dayElsbCount_poof$;
	}
}